package org.shop.controller.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.GoodsClassify;
import org.shop.service.GoodsClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ManagerGoodsClassifyController {

	private static Logger logger = LogManager.getLogger(ManagerGoodsClassifyController.class);
	
	@Autowired
	private GoodsClassifyService goodsClassifyService;
	
	/**
	 * 获取一级所有分类
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/getGoodsClassifyList")
	@ResponseBody
	public List<GoodsClassify> getGoodsClassifyList(HttpServletRequest req){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		List<GoodsClassify> list = null;
		try {
			paramMap.put("parentIdIsNUll", "2");
			list = goodsClassifyService.getGoodsClassifyList(paramMap);
		}catch(Exception e){
			list = null ;
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return list;
	}
	
	
	
	/**
	 * 获取商品分类列表
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/getGoodsClassifyInfoList")
	@ResponseBody
	public Map<String,Object> getGoodsClassifyInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
	 	String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String selectName = req.getParameter("selectName");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", Integer.parseInt(currentPage));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("selectName", selectName);
            paramMap.put("parentIdIsNUll", "2");
            map = goodsClassifyService.getGoodsClassifyInfoList(paramMap);
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	/**
	 * 获取商品子类分类列表
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/getGoodsClassifyChildInfoList")
	@ResponseBody
	public Map<String,Object> getGoodsClassifyChildInfoList(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		String parentId = req.getParameter("parentId");
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("parentId", parentId);
		    List<GoodsClassify> goodsClassifyChildInfoList = goodsClassifyService.getGoodsClassifyChildInfoList(paramMap);
		    map.put("rows", goodsClassifyChildInfoList);
		    map.put("total", goodsClassifyChildInfoList.size());
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	
	/**
	 * 添加一级分类
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/addOneGoodsClassifyInfo")
	@ResponseBody
	public Map<String,Object> addOneGoodsClassifyInfo(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		String cassifyName = req.getParameter("cassifyName");
		try {
			GoodsClassify gcInfo = new GoodsClassify();
			gcInfo.setCassifyName(cassifyName);
			int num = goodsClassifyService.insertSelective(gcInfo );
			map.put("num", num);
		}catch(Exception e) {
			map.put("num", 0);
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	/**
	 * 添加子级分类
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/addTwoGoodsClassifyInfo")
	@ResponseBody
	public Map<String,Object> addTwoGoodsClassifyInfo(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Long cassifyId = Long.parseLong(req.getParameter("cassifyId"));
		String cassifyName = req.getParameter("cassifyName");
		String parentName = req.getParameter("parentName");
		try {
			GoodsClassify gcInfo = new GoodsClassify();
			gcInfo.setParentId(cassifyId);
			gcInfo.setParentName(parentName);
			gcInfo.setCassifyName(cassifyName);
			int num = goodsClassifyService.insertSelective(gcInfo );
			map.put("num", num);
		}catch(Exception e) {
			map.put("num", 0);
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}
	
	/**
	 * 编辑分类名称
	 * @param req
	 * @return
	 */
	@RequestMapping("manager/editGoodsClassifyInfo")
	@ResponseBody
	public Map<String,Object> editGoodsClassifyInfo(HttpServletRequest req){
		Map<String,Object> map = new HashMap<String,Object>();
		Long cassifyId = Long.parseLong(req.getParameter("cassifyId"));
		String cassifyName = req.getParameter("cassifyNameNew");
		try {
			GoodsClassify gcInfo = new GoodsClassify();
			gcInfo.setCassifyId(cassifyId);
			gcInfo.setCassifyName(cassifyName);
			int num = goodsClassifyService.updateByPrimaryKeySelective(gcInfo );
			map.put("num", num);
		}catch(Exception e) {
			map.put("num", 0);
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	} 
	
	
}
